package com.tanhua.server.controller;

import com.tanhua.model.vo.CountsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 查询用户基础信息
     * @param token
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(@RequestHeader("Authorization") String token,Long userID){
        //校验token是否合法
//        boolean b = JwtUtils.verifyToken(token);
//        if (!b){
//            return ResponseEntity.status(401).body(null);
//        }
//        //2、获取当前用户ID
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");

        //判断参数ID是否为空
        if (userID==null){
            userID= UserHolder.getUserId();
        }
        //调用Service查询
        UserInfoVo vo = userInfoService.findById(userID);

        return ResponseEntity.ok(vo);

    }

    @PutMapping
    public ResponseEntity  updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization") String token){
//        //校验token是否合法
//        boolean b = JwtUtils.verifyToken(token);
//        if (!b){
//            return ResponseEntity.status(401).body(null);
//        }
//        //2、获取当前用户ID
//        Claims claims = JwtUtils.getClaims(token);
//        Integer id = (Integer) claims.get("id");
//        userInfo.setId(Long.valueOf(id));
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/header")
    public ResponseEntity UpdateHeader(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws IOException {
        //获取用户ID
        Long userId = UserHolder.getUserId();
        //调用service
        userInfoService.updateHead(headPhoto,userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号码第一步，发送短信验证
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        //获取当前用户手机号
        String phone = UserHolder.getMobile();
        //调用短信服务
        userService.sendCode(phone);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号第二部，校验短信验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map){
        log.info("{}",map);
        String verificationCode = (String) map.get("verificationCode");
        String phone = UserHolder.getMobile();
        boolean verification = userService.check(phone, verificationCode);
        return ResponseEntity.ok(verification);
    }
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map){
        //获取新手机号
        String phone = (String) map.get("phone");
        //调用service修改手机号码
        userService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }


    /**
     * 查询喜欢、互相喜欢、粉丝数量
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
     CountsVo vo  = userService.counts();
        return ResponseEntity.ok(vo);
    }



}
